/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartsserver;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author quangduc
 */
public class ThreadAl extends Thread{
    private Socket socket = null;
    private Al al = null;
    private DataInputStream streamIn = null;
    
      public ThreadAl(Al _al, Socket _socket) {
        al = _al;
        socket = _socket;
        open();
        start();
    }

    public void open() {
        try {
            streamIn = new DataInputStream(socket.getInputStream());
        } catch (IOException ioe) {
            System.out.println("Error getting input stream: " + ioe);
            al.stop();
        }
    }

    public void close() {
        try {
            if (streamIn != null) {
                streamIn.close();
            }
        } catch (IOException ioe) {
            System.out.println("Error closing input stream: " + ioe);
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String strData = streamIn.readUTF();
                //String head = strData.substring(0,1);
                al.XuLyNhanBai(strData);
            } catch (IOException ioe) {
                System.out.println("Listening error: " + ioe.getMessage());
                al.stop();
            }
        }
    }
}
